package com.piao.springboot_rest.controller;

import com.piao.springboot_rest.entity.Word;
import com.piao.springboot_rest.mapper.WordMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

//RestController=@Controller+@ResponseBoby
@RestController
public class MyRestController {
    //存储预返回页面的结果对象
    private Map<String, Object> result;
    //注入业务对象
    @Resource
    private WordMapper wordMapper;

    //1. 添加单词信息
    @RequestMapping(value = "/Word", method = RequestMethod.POST)
    public Map<String, Object> insert(@RequestBody Word word) {
        result = new HashMap<>();
        //数据校验 判空！
        if (null == word) {
            result.put("message", "插入的单词为空");
            return result;
        }
        if (null == word.getChinese() || "".equals(word.getEnglish())) {
            result.put("message", "插入的单词中文为空或者为空字符串");
            return result;
        }
        if (null == word.getChinese() || "".equals(word.getChinese())) {
            result.put("message", "插入的单词中文为空或者为空字符串");
            return result;
        }
        wordMapper.insert(word);
        result.put("message", "插入单词成功");
        return result;
    }

    //2. 根据id删除单词信息
    @RequestMapping(value = "/Word/{id}", method = RequestMethod.DELETE)
    public Map<String, Object> delete(@PathVariable Integer id) {
        result = new HashMap<>();
        //数据校验 判空！
        if (null == id) {
            result.put("message", "输入的单词id为空");
            return result;
        }
        wordMapper.deleteById(id);
        result.put("message", "删除单词成功");
        return result;
    }

    //3. 根据id更新单词信息
    @RequestMapping(value = "/Word", method = RequestMethod.PUT)
    public Map<String, Object> update(@RequestBody Word word) {
        result = new HashMap<>();
        //数据校验 判空！
        if (null == word.getId()) {
            result.put("message", "更新的单词id为空");
            return result;
        }
        if (null == word.getEnglish() || "".equals(word.getEnglish())) {
            result.put("message", "更新的单词中文为空或者为空字符串");
            return result;
        }
        if (null == word.getChinese() || "".equals(word.getChinese())) {
            result.put("message", "更新的单词中文为空或者为空字符串");
            return result;
        }
        wordMapper.updateById(word);
        result.put("message", "更新单词成功");
        return result;
    }

    //4. 根据id查询单词信息
    @RequestMapping(value = "/Word/{id}", method = RequestMethod.GET)
    public Map<String, Object> getOne(@PathVariable Integer id) {
        result = new HashMap<>();
        //数据校验 判空！
        if (null == id) {
            result.put("message", "传入id为空");
            return result;
        }
        result.put("word", wordMapper.selectById(id));
        result.put("message", "查询单词成功");
        return result;
    }

    //5. 查询所有单词
    @RequestMapping(value = "/Word", method = RequestMethod.GET)
    public Map<String, Object> getAll() {
        result = new HashMap<>();
        //selectList(null)设置过滤条件为空，就是查询所有，并以list形式返回
        result.put("word", wordMapper.selectList(null));
        result.put("message", "查询所有单词成功");
        return result;
    }

}